package spike.jdbc.access;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MainMetadata {

	public static void main(String[] args) throws Exception {
		System.out.println("loading driver");
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

		System.out.println("connecting");
		Connection vConnection = DriverManager.getConnection(
				"jdbc:odbc:VEHICULES", "rothlis18", "Duke-123");

		DatabaseMetaData vMetaData = vConnection.getMetaData();

		// Catalogues
		System.out.println("catalogs found");
		ResultSet vCatalogs = vMetaData.getCatalogs();
		while (vCatalogs.next()) {
			String vCatalogName = vCatalogs.getString("TABLE_CAT");
			System.out.println("\t" + vCatalogName);
		}
		vCatalogs.close();

		System.out.println("tables found");
		ResultSet vTables = vMetaData.getTables(null, null, "%", null);
		while (vTables.next()) {
			String vTableName = vTables.getString("TABLE_NAME");
			displayTable(vMetaData, vTableName);
		}
		vTables.close();

		vConnection.close();

		System.out.println("done");
	}

	private static void displayTable(DatabaseMetaData aMetaData,
			String aTableName) throws SQLException {
		System.out.println("\t" + aTableName);

		ResultSet vColumns = aMetaData.getColumns(null, null, aTableName, "%");
		System.out.println("\t" + "columns found :");
		while (vColumns.next()) {
			String vColumnName = vColumns.getString("COLUMN_NAME");
			System.out.println("\t\t" + vColumnName);
		}
		System.out.println();
		vColumns.close();
	}

}
